/*
 * PhoneDict: A J2ME Dict client
 * 
 * Copyright (C) 2006 Labeneator labeneator@gmail.org
 *
 * This file is a part of PhoneDict. PhoneDict is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) any later version.
 * Mobber is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 */

import javax.microedition.lcdui.*;


public class Query extends Form implements CommandListener
{
	TextField txtword, txtdict, txthost, txtport, txtstrategy;
	Command cmddefine, cmdmatch, cmdexit, cmdsrvinfo, cmddblist, cmdstrat, cmddbinfo, cmdsrvstatus, cmdsrvhelp;
	String server = "test.dict.org", port = "2628", dictionary = "*";
	
	Query()
	{
		super("MobileDict");
		
		
		txthost = new TextField("Server", server, 64, TextField.EMAILADDR);
		txtport = new TextField("Server Port",port, 64, TextField.NUMERIC);
		txtdict = new TextField("Dictionary", dictionary, 32, TextField.ANY);
		txtword = new TextField("Word", "penguin", 32, TextField.ANY);
		txtstrategy = new TextField("Strategy", "prefix", 32, TextField.ANY);
/*
		try
        {
                append(new ImageItem(null, Image.createImage("/phonedict.png"), ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_BEFORE | ImageItem.LAYOUT_NEWLINE_AFTER, "PhoneDict"));
        }
        catch(Exception e)
        {
                e.printStackTrace();
        }
*/
		
		cmddefine = new Command("Define",Command.OK,0);
		cmdmatch = new Command("Match",Command.OK,0);
		cmddblist = new Command("Databases List",Command.OK,0);
		cmddbinfo = new Command("Database Info",Command.OK,0);
		cmdstrat = new Command("Strategies",Command.OK,0);
		cmdsrvinfo = new Command("ServerInfo",Command.OK,0);
		cmdsrvstatus = new Command("Server Status",Command.OK,0);
		cmdsrvhelp = new Command("Server Help",Command.OK,0);
		cmdexit = new Command("Exit",Command.EXIT,0);

		append(txtword);
		append(txthost);
		append(txtport);
		append(txtdict);
		append(txtstrategy);
		addCommand(cmddefine);
		addCommand(cmdmatch);
		addCommand(cmdexit);
		addCommand(cmdsrvinfo);
		addCommand(cmddblist);
		addCommand(cmdstrat);
		addCommand(cmddbinfo);
		addCommand(cmdsrvstatus);
		addCommand(cmdsrvhelp);
		setCommandListener(this);
		setTicker(new Ticker(PhoneDict.tickermsg));
	}
	
	public void commandAction(Command c, Displayable s)
	{
		String word = txtword.getString(), dict = txtdict.getString(), 
		server = txthost.getString(), port = txtport.getString(), strategy = txtstrategy.getString();
		if(c == cmddefine)
		{			
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Defining... Please wait...");
			PhoneDict.defn = new Defn(server, port, dict, word);
			System.out.println("In Define");
		}else if (c == 	cmdmatch){
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Matching... Please wait...");
			PhoneDict.match = new Match(server, port, dict, word, strategy);
		}else if (c == cmdsrvinfo){
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Getting Server info...\n Please wait...");
			PhoneDict.srvinfo = new SrvInfo(server, port);
			System.out.println("In Server Info");
		}else if (c == 	cmddblist){
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Getting Database List...\n Please wait...");
			PhoneDict.databases = new Databases(server, port);
		}else if (c == 	cmdstrat){
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Getting Strategies...\n Please wait...");
			PhoneDict.strategies = new Strategies(server, port);
		}else if (c == 	cmddbinfo){
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Getting DatabaseInfo...\n Please wait...");
			PhoneDict.dbquery = new DBQuery(server, port, dict);
		}else if (c == 	cmdsrvstatus){
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Getting Server status...\n Please wait...");
			PhoneDict.srvstatus = new SrvStatus(server, port);
		}else if (c == 	cmdsrvhelp){
			PhoneDict.display.setCurrent(PhoneDict.logger);
			PhoneDict.logger.logging("Getting Server status...\n Please wait...");
			PhoneDict.srvhelp = new Help(server, port);
		}else if (c == cmdexit) {
			PhoneDict.kill();
	    }

	}
};
